<Serializable()> Friend Class Maps

    Private m_width As Short = 0
    Private m_height As Short = 0
    Private m_description As String = vbNullString
    Private m_marginal As Byte = 128
    Private m_firstNode As Map
    Private m_widthScale As Integer = 0
    Private m_widthMultiplier As Double = 1
    Private m_heightMultiplier As Double = 1
    Private m_heightScale As Integer = 0

    Friend Sub New(ByVal width_ As Short, ByVal height_ As Short, ByVal description_ As String, ByVal wScale As Integer, ByVal hScale As Integer)
        m_width = width_
        m_height = height_
        m_widthScale = wScale
        m_heightScale = hScale
        m_description = Trim(description_)
    End Sub
    Friend Sub New(ByVal width_ As Short, ByVal height_ As Short, ByVal description_ As String, ByVal wScale As Integer, ByVal hScale As Integer, ByVal Marginal As Byte)
        m_width = width_
        m_height = height_
        m_widthScale = wScale
        m_heightScale = hScale
        m_description = Trim(description_)
        m_marginal = Marginal
    End Sub
    Friend Sub New(ByVal Description As String)
        m_description = Trim(Description)
    End Sub

    Friend Function addMap(ByRef map_ As Map) As Boolean
        If Not map_.getAddress = 0 And Not map_.getDescription = vbNullString Then
            If IsNothing(m_firstNode) Then
                m_firstNode = map_
            Else
                m_firstNode.addNode(map_)
            End If
            Return True
        Else
            Return False
        End If
    End Function
    Friend Function addMap(ByVal Address_ As Integer, ByVal description_ As String) As Boolean
        description_ = Trim(description_)
        If Not Address_ = 0 And Not description_ = vbNullString And m_width <> 0 And m_height <> 0 Then
            If IsNothing(m_firstNode) Then
                m_firstNode = New Map(Address_, m_width, m_height, description_)
            Else
                m_firstNode.addNode(New Map(Address_, m_width, m_height, description_))
            End If
            Return True
        ElseIf Address_ <> 0 And Trim(description_) <> "" Then
            Dim f_width As Short
            Dim f_height As Short
            If m_width = 0 Then f_width = 1 Else f_width = m_width
            If m_height = 0 Then f_height = 1 Else f_height = m_height
            Return Me.addMap(Address_, description_, f_width, f_height)
        Else
            Return False
        End If
    End Function
    Friend Function addMap(ByVal Address_ As Integer, ByVal description_ As String, ByVal Width_ As Short, ByVal Height_ As Short) As Boolean
        If Not Address_ = 0 And Not description_ = vbNullString And Width_ >= 1 And Height_ >= 1 And m_width = 0 And m_height = 0 And m_widthScale = 0 And m_heightScale = 0 Then
            If IsNothing(m_firstNode) Then
                m_firstNode = New Map(Address_, Width_, Height_, description_)
            Else
                m_firstNode.addNode(New Map(Address_, Width_, Height_, description_))
            End If
            Return True
        Else
            Return False
        End If
    End Function
    Friend Function addMap(ByVal Address_ As Integer, ByVal description_ As String, ByVal Width_ As Short, ByVal Height_ As Short, ByVal Bytesize As Byte) As Boolean
        If Not Address_ = 0 And Not description_ = vbNullString And Width_ >= 1 And Height_ >= 1 And m_width = 0 And m_height = 0 And m_widthScale = 0 And m_heightScale = 0 Then
            If IsNothing(m_firstNode) Then
                m_firstNode = New Map(Address_, Width_, Height_, description_, Bytesize)
            Else
                m_firstNode.addNode(New Map(Address_, Width_, Height_, description_, Bytesize))
            End If
            Return True
        ElseIf Not Address_ = 0 And Not description_ = vbNullString And m_width > 1 And m_height > 1 And Bytesize >= 1 And Bytesize <= 2 Then
            If IsNothing(m_firstNode) Then
                m_firstNode = New Map(Address_, m_width, m_height, description_, Bytesize)
            Else
                m_firstNode.addNode(New Map(Address_, m_width, m_height, description_, Bytesize))
            End If
            Return True
        Else
            Return False
        End If
    End Function

    Friend Sub setWidth(ByVal Width_ As Short)
        m_width = Width_
        If Width_ > 0 And Not IsNothing(m_firstNode) Then
            Dim s_maps() As Map = Me.getAsArray
            For Each s_ptMap As Map In s_maps
                If s_ptMap.getAddress <> m_heightScale Then
                    s_ptMap.setWidth(Width_)
                End If
            Next
        End If
    End Sub
    Friend Sub setHeight(ByVal height_ As Short)
        m_height = height_
        If height_ > 0 And Not IsNothing(m_firstNode) Then
            Dim s_maps() As Map = Me.getAsArray
            For Each s_ptMap As Map In s_maps
                If s_ptMap.getAddress <> m_widthScale Then
                    s_ptMap.setHeight(height_)
                End If
            Next
        End If
    End Sub
    Friend Sub setDescription(ByVal Desc_ As String)
        m_description = Desc_
    End Sub
    Friend Sub setMarginal(ByVal Marginal_ As Byte)
        m_marginal = Marginal_
    End Sub
    Friend Sub setWidthMultiplier(ByVal Mult As Double)
        m_widthMultiplier = Mult
    End Sub
    Friend Sub setHeightMultiplier(ByVal Mult As Double)
        m_heightMultiplier = Mult
    End Sub
    Friend Sub setWidthScale(ByVal Address_ As Integer)
        m_widthScale = Address_
    End Sub
    Friend Sub setWidthScale(ByRef Array_() As Byte, ByRef Source_() As Byte)
        If (UBound(Source_) - LBound(Source_) + 1) = m_width And UBound(Array_) >= (m_widthScale + m_width) Then
            For i As Integer = 0 To m_width - 1
                Array_(m_widthScale + i) = Source_(i)
            Next
        End If
    End Sub
    Friend Sub setWidthScale(ByRef Array_() As Byte, ByRef Source_() As Double)
        If (UBound(Source_) - LBound(Source_) + 1) = m_width And UBound(Array_) >= (m_widthScale + m_width) Then
            For i As Integer = 0 To m_width - 1
                Array_(m_widthScale + i) = iByte(CLng(Source_(i) / m_widthMultiplier))
            Next
        End If
    End Sub
    Friend Sub setHeightScale(ByVal Address_ As Integer)
        m_heightScale = Address_
    End Sub
    Friend Sub setHeightScale(ByRef Array_() As Byte, ByRef Source_() As Byte)
        If (UBound(Source_) - LBound(Source_) + 1) = m_height And UBound(Array_) >= (m_heightScale + m_height) Then
            For i As Integer = 0 To m_height - 1
                Array_(m_heightScale + i) = Source_(i)
            Next
        End If
    End Sub
    Friend Sub setHeightScale(ByRef Array_() As Byte, ByRef Source_() As Double)
        If (UBound(Source_) - LBound(Source_) + 1) = m_height And UBound(Array_) >= (m_heightScale + m_height) Then
            For i As Integer = 0 To m_height - 1
                Array_(m_heightScale + i) = iByte(CLng(Source_(i) / m_heightMultiplier))
            Next
        End If
    End Sub

    Friend Function length() As Integer
        Dim intReturn As Integer = 0
        If Not IsNothing(m_firstNode) Then
            intReturn = m_firstNode.lengthTotal()
        End If
        Return (intReturn)
    End Function
    Friend Function count() As Integer
        Dim intReturn As Integer = 0
        If Not IsNothing(m_firstNode) Then
            intReturn = m_firstNode.count
        End If
        Return intReturn
    End Function
    Friend Function getHeight() As Short
        Return m_height
    End Function
    Friend Function getWidth() As Short
        Return m_width
    End Function
    Friend Function getDescription() As String
        Return m_description
    End Function
    Friend Function getMarginal() As Byte
        Return m_marginal
    End Function
    Friend Function getWidthScale() As Integer
        Return m_widthScale
    End Function
    Friend Function getWidthScale(ByRef Array_() As Byte) As Byte()
        If m_width > 0 And m_widthScale <> 0 Then
            Dim f_array(0 To (m_width - 1)) As Byte
            If UBound(Array_) >= (m_widthScale + m_width) Then
                For i As Integer = 0 To m_width - 1
                    f_array(i) = Array_(m_widthScale + i)
                Next
            End If
            Return f_array
        Else
            Return Nothing
        End If
    End Function
    Friend Function getHeightScale() As Integer
        Return m_heightScale
    End Function
    Friend Function getHeightScale(ByRef Array_() As Byte) As Byte()
        If m_height > 0 And m_heightScale <> 0 Then
            Dim f_array(0 To (m_height - 1)) As Byte
            If UBound(Array_) >= (m_heightScale + m_height) Then
                For i As Integer = 0 To m_height - 1
                    f_array(i) = Array_(m_heightScale + i)
                Next
            End If
            Return f_array
        Else
            Return Nothing
        End If
    End Function
    Friend Function getWidthScaleValue(ByRef Array_() As Byte) As Double()
        Dim f_array(0 To (m_width - 1)) As Double
        If UBound(Array_) >= (m_widthScale + m_width) And m_width > 0 And m_widthScale <> 0 Then
            For i As Integer = 0 To m_width - 1
                f_array(i) = Array_(m_widthScale + i) * m_widthMultiplier
            Next
            Return f_array
        Else
            Return Nothing
        End If
    End Function
    Friend Function getWidthMultiplier() As Double
        Return m_widthMultiplier
    End Function
    Friend Function getHeightMultiplier() As Double
        Return m_heightMultiplier
    End Function
    Friend Function getHeightScaleValue(ByRef Array_() As Byte) As Double()
        Dim f_array(0 To (m_height - 1)) As Double
        If UBound(Array_) >= (m_heightScale + m_height) And m_height > 0 And m_heightScale <> 0 Then
            For i As Integer = 0 To m_height - 1
                f_array(i) = Array_(m_heightScale + i) * m_heightMultiplier
            Next
            Return f_array
        Else
            Return Nothing
        End If
    End Function
    Friend Sub multWidthScale(ByRef Array_() As Byte, ByVal mult_ As Double)
        If UBound(Array_) >= (m_widthScale + m_width) Then
            For i As Byte = 0 To 15
                Array_(m_widthScale + i) = iByte(CLng(mult_ * Array_(m_widthScale + i)))
            Next
        End If
    End Sub
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Friend Function modifyByAddress(ByVal Address_ As Integer, ByVal newDescription As String) As Boolean
        Dim f_cursor As Map
        Dim found As Boolean = False

        If Not IsNothing(m_firstNode) Then
            f_cursor = m_firstNode
            If f_cursor.getAddress = Address_ Then
                f_cursor.setDescription(newDescription)
                found = True
                GoTo endMod
            End If
            While Not IsNothing(f_cursor.getNext) Or Not found
                f_cursor = f_cursor.getNext
                If f_cursor.getAddress = Address_ Then
                    f_cursor.setDescription(newDescription)
                    found = True
                    GoTo endMod
                End If
            End While
        End If
endMod:
        Return found
    End Function
    Friend Function modifyByAddress(ByVal Address_ As Integer, ByVal newAddress As Integer) As Boolean
        Dim f_cursor As Map
        Dim found As Boolean = False

        If IsNothing(getByAddress(newAddress)) Then
            If Not IsNothing(m_firstNode) Then
                f_cursor = m_firstNode
                If f_cursor.getAddress = Address_ Then
                    f_cursor.setAddress(newAddress)
                    found = True
                    GoTo endMod
                End If
                While Not IsNothing(f_cursor.getNext) Or Not found
                    f_cursor = f_cursor.getNext
                    If f_cursor.getAddress = Address_ Then
                        f_cursor.setAddress(newAddress)
                        found = True
                        GoTo endMod
                    End If
                End While
            End If
        End If
endMod:
        Return found
    End Function
    Friend Function modifyByAddress(ByVal Address_ As Integer, ByVal newDescription As String, ByVal newAddress As Integer) As Boolean
        Dim f_cursor As Map
        Dim found As Boolean = False

        If IsNothing(getByAddress(newAddress)) Then
            If Not IsNothing(m_firstNode) Then
                f_cursor = m_firstNode
                If f_cursor.getAddress = Address_ Then
                    f_cursor.setAddress(newAddress)
                    f_cursor.setDescription(newDescription)
                    found = True
                    GoTo endMod
                End If
                While Not IsNothing(f_cursor.getNext) Or Not found
                    f_cursor = f_cursor.getNext
                    If f_cursor.getAddress = Address_ Then
                        f_cursor.setAddress(newAddress)
                        f_cursor.setDescription(newDescription)
                        found = True
                        GoTo endMod
                    End If
                End While
            End If
        End If
endMod:
        Return found
    End Function
    Friend Function getByAddress(ByVal Address_ As Integer) As Map
        Dim f_cursor As Map
        Dim found As Boolean = False
        If Not IsNothing(m_firstNode) Then
            f_cursor = m_firstNode
            If f_cursor.getAddress = Address_ Then
                found = True
                GoTo endMod
            End If
            While Not found
                If Not IsNothing(f_cursor.getNext) Then
                    f_cursor = f_cursor.getNext
                    If f_cursor.getAddress = Address_ Then
                        found = True
                        GoTo endMod
                    End If
                Else
                    found = False
                    GoTo endMod
                End If
            End While
        Else
            Return Nothing
        End If
endMod:
        If found Then Return f_cursor Else Return Nothing
    End Function
    Friend Function getByIndex(ByVal Index_ As Integer) As Map
        Dim f_cursor As Map
        Dim found As Boolean = False
        If Not IsNothing(m_firstNode) Then
            f_cursor = m_firstNode
            If Index_ = 0 Then
                Return f_cursor
                found = True
            Else
                For count As Integer = 1 To Index_
                    If Not IsNothing(f_cursor.getNext) Then
                        f_cursor = f_cursor.getNext
                        found = True
                    Else
                        Return Nothing
                        Exit For
                    End If
                Next
            End If
        Else
            Return Nothing
        End If
        If found = False Then Return Nothing Else Return f_cursor
    End Function
    Friend Function getAsArray() As Map()
        Dim f_count As Integer
        Dim f_mapArray() As Map = Nothing
        If Not IsNothing(m_firstNode) Then
            f_count = m_firstNode.count
            ReDim f_mapArray(0 To f_count - 1)
            For i As Integer = 0 To f_count - 1
                f_mapArray(i) = Me.getByIndex(i)
            Next
        End If
        Return f_mapArray
    End Function
    Friend Function removeByAddress(ByVal Address_ As Integer) As Boolean
        Dim s_first As Map
        Dim s_temp As Map
        Dim found As Boolean = False

        If Not IsNothing(m_firstNode) Then
            If m_firstNode.getAddress = Address_ Then
                If IsNothing(m_firstNode.getNext) Then
                    m_firstNode = Nothing
                Else
                    s_temp = m_firstNode.getNext
                    m_firstNode = Nothing
                    Me.addMap(s_temp)
                End If
            Else
                ' First node not a match.. now check all other nodes
                s_first = m_firstNode
                Do
                    If s_first.getNext.getAddress = Address_ Then
                        'next node is a match..
                        If IsNothing(s_first.getNext.getNext) Then
                            'next node has no next.. so just null delete the link
                            s_first.setNext(Nothing)
                            found = True
                            GoTo endSub
                        Else
                            'the next node has a next.. so must swap the links
                            s_temp = s_first.getNext.getNext
                            s_first.setNext(s_temp)
                            found = True
                            GoTo endSub
                        End If
                    End If
                    s_first = s_first.getNext
                Loop Until found Or IsNothing(s_first.getNext)
            End If
        End If
endSub:
        s_first = Nothing
        s_temp = Nothing
        If found Then Return True Else Return False
    End Function
    Friend Function removeByIndex(ByVal Index_ As Integer) As Boolean
        Dim ret As Boolean = False
        Dim f_address As Integer
        Dim f_map As Map
        f_map = Me.getByAddress(Index_)
        If Not IsNothing(f_map) Then
            f_address = f_map.getAddress()
            ret = Me.removeByAddress(f_address)
        End If
        Return ret
    End Function
    Friend Function getFirst() As Map
        Return m_firstNode
    End Function

    Friend Function interNewScales(ByRef Array_() As Byte, ByRef newWidthScale() As Double, ByRef newHeightScale() As Double) As Boolean
        Dim f_return As Boolean = False
        If (UBound(newWidthScale) - LBound(newWidthScale) + 1) = m_width And UBound(Array_) >= (m_widthScale + m_width) And _
            (UBound(newHeightScale) - LBound(newHeightScale) + 1) = m_height And UBound(Array_) >= (m_heightScale + m_height) Then
            Dim newWidth(0 To UBound(newWidthScale)) As Byte
            Dim newHeight(0 To UBound(newHeightScale)) As Byte
            For i As Integer = 0 To UBound(newWidthScale)
                newWidth(i) = iByte(CLng(newWidthScale(i) / Me.getWidthMultiplier))
            Next
            For i As Integer = 0 To UBound(newHeightScale)
                newHeight(i) = iByte(CLng(newHeightScale(i) / Me.getHeightMultiplier))
            Next
            f_return = Me.interNewScales(Array_, newWidth, newHeight)
        End If
        Return f_return
    End Function
    Friend Function interNewScales(ByRef Array_() As Byte, ByRef newWidthScale() As Byte, ByRef newHeightScale() As Byte) As Boolean
        Dim f_return As Boolean = False
        Dim f_index As Integer = 0
        If (UBound(newWidthScale) - LBound(newWidthScale) + 1) = m_width And UBound(Array_) >= (m_widthScale + m_width) And _
            (UBound(newHeightScale) - LBound(newHeightScale) + 1) = m_height And UBound(Array_) >= (m_heightScale + m_height) Then

            ' Setup the Current Scales
            Dim iWidth() As Byte = iCreateAxis(Me.getWidthScale(Array_), Me.getWidth)
            Dim iHeight() As Byte = iCreateAxis(Me.getHeightScale(Array_), Me.getHeight)
            Dim iNewWidth() As Byte = iCreateAxis(newWidthScale, Me.getWidth)
            Dim iNewHeight() As Byte = iCreateAxis(newHeightScale, Me.getHeight)

            Dim iDataGrid(,) As Byte
            Dim iResultGrid() As Byte
            'Dim iMap As Map

            'While Not IsNothing(Me.getByIndex(f_index))
            For Each iMap As Map In Me.getAsArray
                'iMap = Me.getByIndex(f_index)
                If iMap.getWidth = Me.getWidth And iMap.getHeight = Me.getHeight Then
                    iDataGrid = iCreateGrid(iMap.getMap(Array_), Me.getWidth, Me.getHeight)
                    iResultGrid = iData(iWidth, iHeight, iNewWidth, iNewHeight, iDataGrid, Me.getMarginal)
                    iMap.setMap(Array_, iResultGrid)
                End If
                'f_index += 1
            Next
            'End While

            f_return = True

            Me.setWidthScale(Array_, newWidthScale)
            Me.setHeightScale(Array_, newHeightScale)
        End If
        Return f_return
    End Function
    Private Function iCreateGrid(ByRef Arra() As Byte, ByVal X As Short, ByVal Y As Short) As Byte(,)
        Dim f As Integer
        Dim i As Integer
        Dim iGrid(,) As Byte
        ReDim iGrid(0 To X + 1, 0 To Y + 1)
        For i = 0 To X - 1
            For f = 0 To Y - 1
                iGrid(f + 1, i + 1) = Arra((i * X) + f)
            Next
        Next
        iGrid(0, 0) = iGrid(1, 1)
        iGrid(17, 17) = iGrid(16, 16)
        iGrid(17, 0) = iGrid(16, 1)
        iGrid(0, 17) = iGrid(1, 16)
        For i = 1 To X
            iGrid(i, 0) = iGrid(i, 1)
            iGrid(i, 17) = iGrid(i, 16)
        Next
        For f = 1 To Y
            iGrid(0, f) = iGrid(1, f)
            iGrid(17, f) = iGrid(16, f)
        Next
        Return iGrid
    End Function
    Private Function iValue(ByRef TP() As Byte, ByRef RPM() As Byte, ByRef vTP As Byte, ByRef vRPM As Byte, ByRef data(,) As Byte, ByRef Marginal As Byte) As Byte
        Dim f, i As Byte
        Dim Y, X As Integer
        Dim Xdiff, Ydiff, XstartPt, YstartPt As Double
        Dim sub_data(0 To 1, 0 To 1) As Byte
        Dim sub_pts(0 To 1) As Byte
        Dim iPts(0 To 1) As Byte

        X = 0
        Do While vTP >= TP(X)
            X = X + 1
        Loop
        Xdiff = TP(X) - TP(X - 1)
        XstartPt = vTP - TP(X - 1)
        X = X - 1

        Y = 0
        Do While vRPM >= RPM(Y)
            Y = Y + 1
        Loop
        Ydiff = RPM(Y) - RPM(Y - 1)
        YstartPt = vRPM - RPM(Y - 1)
        Y = Y - 1

        For i = 0 To 1
            For f = 0 To 1
                sub_data(f, i) = data(f + X, i + Y)
            Next
        Next

        If XstartPt > 0 And YstartPt > 0 And XstartPt < Xdiff And YstartPt < Ydiff Then
            iSubValue(sub_data, Marginal)
        End If

        If YstartPt = 0 And XstartPt = 0 Then
            iValue = sub_data(0, 0)
            Exit Function
        ElseIf YstartPt = 0 Then
            sub_pts(0) = sub_data(0, 0)
            sub_pts(1) = sub_data(1, 0)
            iValue = TwoPtInter(XstartPt, Xdiff, sub_pts, Marginal)
            Exit Function
        ElseIf XstartPt = 0 Then
            sub_pts(0) = sub_data(0, 0)
            sub_pts(1) = sub_data(0, 1)
            iValue = TwoPtInter(YstartPt, Ydiff, sub_pts, Marginal)
            Exit Function
        Else
            sub_pts(0) = sub_data(0, 0)
            sub_pts(1) = sub_data(1, 0)
            iPts(0) = TwoPtInter(XstartPt, Xdiff, sub_pts, Marginal)
            sub_pts(0) = sub_data(0, 1)
            sub_pts(1) = sub_data(1, 1)
            iPts(1) = TwoPtInter(XstartPt, Xdiff, sub_pts, Marginal)
            iValue = TwoPtInter(YstartPt, Ydiff, iPts, Marginal)
            Exit Function
        End If
    End Function
    Private Sub iSubValue(ByRef sData(,) As Byte, ByRef Marginal_ As Byte)
        If Marginal_ <> 0 Then
            Debug.Print(Marginal_)
            If sData(0, 0) >= 128 Or sData(0, 1) >= 128 Or sData(1, 0) >= 128 Or sData(1, 1) >= 128 Then
                If sData(0, 0) <= 127 Or sData(0, 1) <= 127 Or sData(1, 0) <= 127 Or sData(1, 1) <= 127 Then
                    For i As Byte = 0 To 1
                        For f As Byte = 0 To 1
                            If sData(f, i) >= 128 Then
                                Debug.Print("setting down from marg")
                                sData(f, i) = iByte((sData(f, i) - 64) - Marginal_)
                            End If
                        Next
                    Next
                End If
            End If
        End If
    End Sub
    Private Function TwoPtInter(ByRef SplitPt As Double, ByRef Diff As Double, ByRef pts() As Byte, ByRef Marginal_ As Byte) As Byte
        Dim pt As Byte

        If (pts(0) < 128 And pts(1) < 128) Or (pts(0) > 127 And pts(1) > 127) Then
            pt = iByte(CLng((((SplitPt / Diff) * pts(1)) + ((Diff - SplitPt) / Diff) * pts(0))))
        Else
            If pts(0) > 128 Then
                pts(0) = iByte(pts(0) - Marginal_)
                If pts(0) < 0 Then
                    pts(0) = 0
                End If
            End If
            If pts(1) > 128 Then
                pts(1) = pts(1) - Marginal_
                If pts(1) < 0 Then
                    pts(1) = 0
                End If
            End If

            pt = iByte(CLng((((SplitPt / Diff) * pts(1)) + ((Diff - SplitPt) / Diff) * pts(0))))
            'If pt > 255 Then
            ' TwoPtInter = 255
            ' ElseIf pt < 0 Then
            ' TwoPtInter = Marginal_ + pt
            ' Else
            ' TwoPtInter = pt
            'End If
        End If
        Return pt
    End Function
    Private Function iData(ByRef TP() As Byte, ByRef RPM() As Byte, ByRef TPnew() As Byte, ByRef RPMnew() As Byte, ByRef data(,) As Byte, ByRef Marginal As Byte) As Byte()
        Dim X As Integer
        Dim Y As Integer
        Dim iDataRet() As Byte
        ReDim iDataRet(0 To (UBound(TP) - 1) * (UBound(RPM) - 1) - 1)

        For Y = LBound(RPM) + 1 To UBound(RPM) - 1
            For X = LBound(TP) + 1 To UBound(TP) - 1
                iDataRet(((Y - 1) * (UBound(TP) - 1)) + (X - 1)) = iValue(TP, RPM, TPnew(X), RPMnew(Y), data, Marginal) 'x% - 1, Y% - 1) = iValue(TP, RPM, TPnew(x%), RPMnew(Y%), data, Marginal)
            Next
        Next
        Return iDataRet
    End Function
    Private Function iCreateAxis(ByRef Arra() As Byte, ByVal Length_ As Short) As Byte()
        Dim iAxis(0 To Length_ + 1) As Byte
        For f As Integer = 0 To Length_ - 1
            iAxis(f + 1) = Arra(f)
        Next
        iAxis(0) = 0
        iAxis(17) = 255
        Return iAxis
    End Function
End Class